package it.blueocean.acanto.entities.platform.security;

import it.blueocean.acanto.entities.platform.Group;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

@Entity
@Table(name = "TB_AUTHORITIES")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Authority implements Serializable
{
	private static final long serialVersionUID = -6898449837079676215L;

	public Authority()
	{
	}
	
	public Authority(String authority)
	{
		this.authority = authority;
	}
	
	@Id
	@Column(name = "AUTHORITY", columnDefinition = "VARCHAR(50) NOT NULL")
	private String authority;
	
	@Column(name = "DESCRIPTION", length = 50)
	private String description;

//	@ManyToMany(fetch = FetchType.EAGER, targetEntity = Group.class)
//	@JoinTable(name="TB_GROUPS_AUTHORITIES", 
//			joinColumns={@JoinColumn(name="AUTHORITY")},
//			inverseJoinColumns = {@JoinColumn(name = "GROUP_ID")})
//	@Fetch(FetchMode.SUBSELECT)
	
	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "TB_GROUPS_AUTHORITIES", 
			joinColumns = { @JoinColumn(name = "AUTHORITY", nullable = false, updatable = false) }, 
			inverseJoinColumns = { @JoinColumn(name = "GROUP_ID", nullable = false, updatable = false) }
	)
	private Set<Group> groups = new HashSet<Group>();

	public Set<Group> getGroups()
	{
		return groups;
	}

	public void setGroups(Set<Group> groups)
	{
		this.groups = groups;
	}

	public String getAuthority()
	{
		return authority;
	}

	public void setAuthority(String authority)
	{
		this.authority = authority;
	}

	public String toString()
	{
		StringBuffer sb = new StringBuffer();
		sb.append("authority : ").append(this.authority).append("; ");

		return sb.toString();
	}

	public String getDescription()
	{
		return description;
	}

	public void setDescription(String description)
	{
		this.description = description;
	}
	
	
}
